﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PServiceBus.Core.Interface;
using Phoenix.ZeroMQConnectors;
using PServiceBus.Core.Logger;
using System.Collections.Concurrent;
using PServiceBus.Services.Gateway.Runtime;
using PServiceBus.Core.Security;
using PServiceBus.Core.Manager;
using PServiceBus.Services.Gateway.Extensions;
using PServiceBus.Gateway.Interfaces;
using PServiceBus.Gateway.Interfaces.DTO;
using PServiceBus.Gateway.Interfaces.Commands;
using PServiceBus.Core.Runtime;

namespace PServiceBus.Services.Gateway.Services {
    public sealed class MessageBusGateway : MessageServiceBase<BusMessage> {

       
        public MessageBusGateway(string address) : base(address) { }

        protected override void RegisterRequestResponse() {
            var gateway = ServiceLocator<IGatewayService>.Get();
            if (gateway == null)
                throw new InvalidOperationException("ServiceLocator could not find an implementation of IGatewayService.");
            gateway.Server = Server;
            gateway.RegisterRequestResponse();
        }

        protected override void RegisterCommands() {
            RegisterCommand(CreateSubscriber);
            RegisterCommand(ChangeSubscriberDurability);
            RegisterCommand(SubscribeTo);
            RegisterCommand(UpdateSubscriber);
            RegisterCommand(UnSubscribeFrom);
            RegisterCommand(AddTransport);
            RegisterCommand(DeleteTransport);
            RegisterCommand(DeleteSubscriber);
            RegisterCommand(RegisterTopic);
            RegisterCommand(PublishTopic);
            RegisterCommand(DeleteTopic);
        }

        private static void CreateSubscriber(BusMessage command) {
            GetMessageBus(command.UserContext, reThrowException: command.ReThrowException, headers: command.Headers).CreateSubscriber(command.SubscriberName, command.Headers);
        }

        private static void ChangeSubscriberDurability(BusMessage command) {
            GetMessageBus(command.UserContext, reThrowException: command.ReThrowException, headers: command.Headers).ChangeSubscriberDurability(command.SubscriberName,
                command.Durable, command.Headers);
        }

        private static void SubscribeTo(BusMessage command) {
            GetMessageBus(command.UserContext, reThrowException: command.ReThrowException, headers: command.Headers).SubscribeTo(command.SubscriberName,
                command.TopicName, command.Filter, command.NeedHeader, command.CaseSensitive, command.Headers);
        }

        private static void UpdateSubscriber(BusMessage command) {
            GetMessageBus(command.UserContext, reThrowException: command.ReThrowException, headers: command.Headers).UpdateSubscriber(command.SubscriberName,
                command.TopicName, command.Filter, command.NeedHeader, command.CaseSensitive, command.Headers);
        }

        private static void UnSubscribeFrom(BusMessage command) {
            GetMessageBus(command.UserContext, reThrowException: command.ReThrowException, headers: command.Headers).UnSubscribeFrom(command.SubscriberName,
                command.TopicName, command.Headers);
        }

        private static void AddTransport(BusMessage command) {
            GetMessageBus(command.UserContext, reThrowException: command.ReThrowException, headers: command.Headers).AddTransport(command.SubscriberName,
                command.TransportName, command.Transport, command.TopicName, command.Headers);
        }

        private static void DeleteTransport(BusMessage command) {
            GetMessageBus(command.UserContext, reThrowException: command.ReThrowException, headers: command.Headers).DeleteTransport(command.SubscriberName,
                command.TransportName, command.Headers);
        }

        private static void DeleteSubscriber(BusMessage command) {
            GetMessageBus(command.UserContext, reThrowException: command.ReThrowException, headers: command.Headers).DeleteSubscriber(command.SubscriberName, command.Headers);
        }

        private static void RegisterTopic(BusMessage command) {
            GetMessageBus(command.UserContext, reThrowException: command.ReThrowException, headers: command.Headers).RegisterTopic(command.Topic, command.Headers);
        }

        private static void PublishTopic(BusMessage command) {
            GetMessageBus(command.UserContext, reThrowException: command.ReThrowException, headers: command.Headers).PublishTopic(command.Topic, command.Headers);
        }

        private static void DeleteTopic(BusMessage command) {
            GetMessageBus(command.UserContext, reThrowException: command.ReThrowException, headers: command.Headers).DeleteTopic(command.TopicName, command.Headers);
        }
    }
}
